package org.gebze.Util;

import com.asprise.util.jtwain.JTwainException;
import com.asprise.util.jtwain.Source;
import com.asprise.util.jtwain.SourceManager;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import javax.imageio.ImageIO;

/**
 * Tarama işlemini yapan class
 *
 * @author Özcan ÇAĞIRICI <ozcan.cagirici@gmail.com>
 *
 * @author Hatice Şahin <haticesahin.09@gmail.com>
 */
public class Scanner {

    private Source source = null;

    public Scanner() throws JTwainException {
        source = SourceManager.instance().getDefaultSource();
    }

    /**
     * Tarama işlemini yapar ve datayı byte[] olarak getirir.
     *
     * @return
     */
    public byte[] getImage() throws JTwainException {

        ByteArrayOutputStream bos = new ByteArrayOutputStream();

        source.open();
        System.out.println(source + " isimli cihaz acildi");
        BufferedImage image = source.acquireImageAsBufferedImage();

        try {
            ImageIO.write(image, "jpg", bos);
        } catch (IOException ex) {
            System.out.println("Dosya olusturulurken hata meydana geldi: " + ex);
            return null;
        }
        return bos.toByteArray();


    }
}